<?php

namespace App\Http\Controllers\Auth;

use App\Exceptions\ApiException;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Controller;
use App\Http\Model\AdminModel;
use App\Http\Model\AdminSessionModel;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /**
     * 项目登录
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loginProject(Request $request)
    {
        return $this->login($request,AdminController::PROJECT);
    }

    /**
     * 公司登录
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loginCompany(Request $request)
    {
        return $this->login($request,AdminController::COMPANY);
    }

    /**
     * 登录
     * @param Request $request
     * @param $isProject
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function login(Request $request,$isProject)
    {
        $rules = [
            'username' => 'required',
            'password' => 'required',
        ];
        $message = [
            'username.required' => '请填写用户名',
            'password.required' => '请填写密码',
        ];
        $param = $request->only(['username', 'password']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $adminModel = new AdminModel();
            $adminInfo = $adminModel->info(['username' => $param['username']]);
            $password = $this->encrypt($param['password']);
            if (empty($adminInfo) || $password != $adminInfo['password']) {
                throw new ApiException('账号或密码不正确',110102);
            }
            if ($adminInfo['status'] == 0){
                throw new ApiException('账号已经停用',110103);
            }
            if($isProject == AdminController::PROJECT){
                if (is_null($adminInfo['projectId']) || $adminInfo['projectId'] == 1){
                    throw new ApiException('此账号不能登录项目管理系统',110104);
                }

            }elseif ($isProject == AdminController::COMPANY){
                if ($adminInfo['projectId'] > 1){
                    throw new ApiException('此账号不能登录公司管理系统',110105);
                }
            }

            //获取用户角色
            $adminController = new AdminController();
            $roleInfo = $adminController->getRole($adminInfo['id']);
            //获取用户权限
            $roleId = isset($roleInfo['roleId']) ? $roleInfo['roleId'] : 0;
            $adminInfo['permission'] = $adminController->getPermission($adminInfo['id'], $roleId,$isProject);
            $adminInfo['menu'] = $adminController->getMenu($adminInfo['permission']);
            $token = $this->tokenGenerator();
            $adminInfo['token'] = $token;
            $adminInfo['roleId'] =$roleId;
            AdminSessionModel::put($token, $adminInfo['id'], $adminInfo['projectId']);
            $this->data = $adminInfo;
        } else {
            throw new ApiException($validator->errors()->first(),110101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 系统登出
     *
     * @param Request $request
     * @return mixed
     */
    public function logout(Request $request)
    {
        AdminSessionModel::delete($request->only(config('yucheng.token')));
        return $this->ajaxResult($this->code, '成功退出', $this->data);
    }
}
